package dongze.com.ckdj.adapter;

import android.content.Context;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import dongze.com.ckdj.R;
import dongze.com.ckdj.service.ImageUploadTask;

/**
 * Created by fanqwz on 2017/10/12.
 */

public class UploadImageDialogAdapter extends BaseAdapter {
    public enum STATE {STATE_WAITING,STATE_UPLOAD, STATE_FAIL, STATE_FINISHED}

    ;
    List<ImageUploadTask> taskList;
    List<File> images;
    List<STATE> states;
    List<Integer> progress;
    OnTaskActionListener onTaskFailListener;

    public UploadImageDialogAdapter(List<ImageUploadTask> tasks, List<File> images, OnTaskActionListener listener) {
        taskList = tasks;
        this.images = images;
        onTaskFailListener = listener;
        progress = new ArrayList<>(tasks.size());
        for (int i = 0; i < tasks.size(); i++) {
            progress.add(0);
        }
        states = new ArrayList<>(tasks.size());
        for (int i = 0; i < tasks.size(); i++) {
            states.add(STATE.STATE_UPLOAD);
        }
    }

    public void updateState(long id, STATE state) {
        for (int i = 0; i < taskList.size(); i++) {
            if (taskList.get(i).getId() == id) {
                states.set(i, state);

                notifyDataSetChanged();
                break;
            }
        }

        if (state == STATE.STATE_FINISHED)
            checkIfAllFinished();
    }

    public void updateProgress(long id, int progress) {
        for (int i = 0; i < taskList.size(); i++) {
            if (taskList.get(i).getId() == id) {
                this.progress.set(i, progress);

                notifyDataSetChanged();
                break;
            }
        }
    }

    private void checkIfAllFinished() {
        boolean finished = true;
        for (STATE s :
                states) {
            if (s != STATE.STATE_FINISHED) {
                finished = false;
                break;
            }
        }

        if (finished)
            onTaskFailListener.allFinished();
    }

    @Override
    public int getCount() {
        return taskList.size();
    }

    @Override
    public Object getItem(int position) {
        return taskList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_upload_image_list_item, parent, false);
            Holder holder = new Holder();
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.textView = (TextView) convertView.findViewById(R.id.title);
            holder.button = (ImageButton) convertView.findViewById(R.id.upload_action);
            holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progress);
            holder.state = (ImageView) convertView.findViewById(R.id.upload_state);
            convertView.setTag(holder);
        }

        Holder holder = (Holder) convertView.getTag();
        Glide.with(holder.img.getContext())
                .load(images.get(position))
                .into(holder.img);

        holder.textView.setText(getStateText(states.get(position)));
        holder.state.setImageDrawable(getStateDrawable(holder.button.getContext(), states.get(position)));
        holder.progressBar.setProgress(progress.get(position));

        if (states.get(position) == STATE.STATE_FAIL) {
            holder.state.setVisibility(View.GONE);
            holder.button.setVisibility(View.VISIBLE);
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTaskFailListener != null)
                        onTaskFailListener.restartTask(taskList.get(position).getId());
                }
            });
        } else {
            holder.state.setVisibility(View.VISIBLE);
            holder.button.setVisibility(View.GONE);
        }

        return convertView;
    }

    private String getStateText(STATE t) {
        switch (t) {
            case STATE_WAITING:
                return "等待上传";
            case STATE_UPLOAD:
                return "上传中";
            case STATE_FAIL:
                return "上传失败,请重新上传";
            case STATE_FINISHED:
                return "上传完成";
        }

        return "";
    }

    private Drawable getStateDrawable(Context context, STATE state) {
        switch (state) {
            case STATE_UPLOAD:
                AnimatedVectorDrawable d = (AnimatedVectorDrawable) context.getResources().getDrawable(R.drawable.ic_uploading_animed);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    d.start();
                }
                return d;

            case STATE_FINISHED:
                return context.getResources().getDrawable(R.drawable.ic_ok);
        }

        return null;
    }

    private class Holder {
        ImageView img;
        TextView textView;
        ImageButton button;
        ImageView state;
        ProgressBar progressBar;
    }

    public interface OnTaskActionListener {
        void restartTask(long id);

        void allFinished();
    }
}
